package com.steven.tool.soapTest;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.endpoint.Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.namespace.QName;

public class SoapUtil {

    private final static Logger logger = LoggerFactory.getLogger(SoapUtil.class);

/*    //南华
   public static final String wsdl_url ="http://y4g2pq.natappfree.cc/ts-platform-work/soap/digitalObstetrics?wsdl";
   public static final String wsdl_method ="getRegistListRequest";
   public static final String wsdl_nameSpace ="digitalObstetrics";*/

/*    //同济
    public static final String wsdl_url ="http://localhost:8898/webservice/xmlDataSyn.cxf?wsdl";
    public static final String wsdl_method ="xmlDataSyn";
    public static final String wsdl_nameSpace ="http://ogis.yyt.com";*/

    //省平台
    public static final String wsdl_url ="http://17o8232j73.imwork.net:26030/csp/hsb/DHC.Published.PUB0053.BS.PUB0053.CLS?WSDL=1";
    public static final String wsdl_method ="HIPMessageServer";
    public static final String wsdl_nameSpace ="http://tempuri.org";


    public static String getSsoResult(String params)  {
        String result="";
        try{
//            result = (String)callCommon(wsdl_url, wsdl_nameSpace, wsdl_method, params)[0];
            result = (String)callCommon_more(wsdl_url, wsdl_nameSpace, wsdl_method, "CqYytDataInfo","4101","00",params)[0];
            logger.info("返回数据"+result);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return  result;
    }

    public static void main(String[] args) {
        String result = null;
        try {
            result = (String)callCommon_more(wsdl_url, wsdl_nameSpace, wsdl_method, "<YYTEnvelope><MsgType>B003</MsgType><MsgContent><patient_id>0002222272</patient_id></MsgContent></YYTEnvelope>")[0];
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);;
    }





    /**
     *
     * @param wsdlUrl 调用wsdl地址
     * @param QName 命名空间
     * @param methodName 调用方法
     * @return
     * @throws Exception
     */
    public static Object[] callCommon( String wsdlUrl,String QName,String methodName, String arg0) throws Exception {
        if (org.apache.commons.lang3.StringUtils.isBlank(methodName)) {
            throw new Exception("未指定调用远程接口的方法");
        }
        //在一个方法中连续调用多次WebService接口,每次调用前需要重置上下文
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(wsdlUrl);
        Thread.currentThread().setContextClassLoader(cl);
        logger.debug("正在调用{}接口，详细参数：{}", methodName, arg0);
        if (StringUtils.isNotBlank(QName)){
            return client.invoke(new QName(QName, methodName), arg0);
        }else{
            return client.invoke(methodName,arg0);
        }
    }


    /**
     *
     * @param wsdlUrl 调用wsdl地址
     * @param QName 命名空间
     * @param methodName 调用方法
     * @return
     * @throws Exception
     */
    public static Object[] callCommon_more( String wsdlUrl,String QName,String methodName, Object... var2) throws Exception {
        if (org.apache.commons.lang3.StringUtils.isBlank(methodName)) {
            throw new Exception("未指定调用远程接口的方法");
        }
        //在一个方法中连续调用多次WebService接口,每次调用前需要重置上下文
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(wsdlUrl);
        Thread.currentThread().setContextClassLoader(cl);
        logger.debug("正在调用{}接口，详细参数：{}", methodName, var2);
        if (StringUtils.isNotBlank(QName)){
            return client.invoke(new QName(QName, methodName), var2);
        }else{
            return client.invoke(methodName,var2);
        }
    }





}
